package com.triones.yjj.controller.web;

import cn.dev33.satoken.annotation.SaCheckPermission;
import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.util.ObjectUtil;
import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.PageQuery;
import com.ruoyi.common.core.domain.R;
import com.ruoyi.common.core.domain.entity.SysDept;
import com.ruoyi.common.core.domain.entity.SysUser;
import com.ruoyi.common.core.domain.model.LoginUser;
import com.ruoyi.common.core.page.TableDataInfo;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.common.exception.ServiceException;
import com.ruoyi.common.helper.LoginHelper;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.system.domain.vo.SysUserExportVo;
import com.triones.yjj.entity.WithdrawalRecordDO;
import com.triones.yjj.entity.bo.ActivityManageDetailParams;
import com.triones.yjj.entity.bo.WithdrawalListParams;
import com.triones.yjj.entity.bo.WithdrawalRecordBO;
import com.triones.yjj.entity.vo.ActivityOrderListVO;
import com.triones.yjj.entity.vo.WithdrawalRecordCustomVO;
import com.triones.yjj.entity.vo.WithdrawalRecordExport;
import com.triones.yjj.entity.vo.WithdrawalRecordVO;
import com.triones.yjj.service.SettledStoreService;
import lombok.RequiredArgsConstructor;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletResponse;
import java.util.List;

/**
 * 特约商户
 *
 * @Author: triones
 */
@Validated
@RequiredArgsConstructor
@RestController
@RequestMapping("/system/settledStore")
public class SettledStoreController extends BaseController {

    private final SettledStoreService settledStoreService;

    /**
     * 查询系统用户列表
     */
    @GetMapping("/sysUser")
    public R<List<SysUser>> sysUserList() {
        return R.ok(settledStoreService.sysUserList());
    }

    /**
     * 提现明细（分页）
     */
    @GetMapping("/withdrawal/pageList")
    public TableDataInfo<WithdrawalRecordCustomVO> withdrawalPageList(WithdrawalListParams params, PageQuery pageQuery) {
        return settledStoreService.withdrawalPageList(params, pageQuery);
    }

    /**
     * 完成打款
     */
    @PostMapping("/done")
    public R<Void> doneWithdrawal (@RequestBody WithdrawalRecordBO bo) {
        LoginUser loginUser = LoginHelper.getLoginUser();
        if (loginUser == null) {
            throw new ServiceException("当前用户退出登录!");
        }
        return toAjax(settledStoreService.doneWithdrawal(bo));
    }

    /**
     * 导出提现申请列表
     */
    @PostMapping("/export")
    public void export(WithdrawalListParams params, HttpServletResponse response) {
        List<WithdrawalRecordCustomVO> list = settledStoreService.selectExportList(params);
        List<WithdrawalRecordExport> listVo = BeanUtil.copyToList(list, WithdrawalRecordExport.class);
        for (int i = 0; i < list.size(); i++) {
            Integer status = list.get(i).getStatus();
            WithdrawalRecordExport withdrawalRecordExport = listVo.get(i);
            if (ObjectUtil.isNotEmpty(status)) {
                if (status == 1) {
                    withdrawalRecordExport.setStatus("处理中");
                } else if (status == 2) {
                    withdrawalRecordExport.setStatus("已到账");
                } else if (status == 3) {
                    withdrawalRecordExport.setStatus("提现失败");
                } else if (status == 4) {
                    withdrawalRecordExport.setStatus("商家撤销");
                } else if (status == 5) {
                    withdrawalRecordExport.setStatus("银行审批");
                } else if (status == 6) {
                    withdrawalRecordExport.setStatus("驳回申请");
                } else {
                    withdrawalRecordExport.setStatus("状态异常，请检查");
                }
            }
        }
        ExcelUtil.exportExcel(listVo, "提现申请数据", WithdrawalRecordExport.class, response);
    }
}
